import Vue from 'vue'
import * as Util from '../components/Util'
import co from 'co'
import AppData from '../stores/AppData'

let printKingTaxBillGen = function * (data) {
  let tax = yield co(Vue.resetpost('rs/sql/singleTable_OrderBy', {data: {items: '*', tablename: 't_userinfo',
  condition:'f_userinfo_id = ' + "'" + data.f_userinfo_id + "'",
               orderitem: 'f_userinfo_id'}}, {resolveMsg: null, rejectMsg: null}))
  let params = {
    InfoKind: data.f_bill_style[0],
    InfoSellerAddressPhone: AppData.getSingleValue('售方地址及电话'),
    InfoSellerBankAccount: AppData.getSingleValue('售方开户行及账号'),
    InfoClientName: tax.data[0].f_user_name ? tax.data[0].f_user_name : '',
    InfoClientTaxCode: tax.data[0].f_taxpayer_id ? tax.data[0].f_taxpayer_id : '',
    InfoClientBankAccount: tax.data[0].f_paper_account ? tax.data[0].f_paper_account : '',
    InfoClientAddressPhone: tax.data[0].f_address_phone ? tax.data[0].f_address_phone : '',
    InfoCashier: AppData.getSingleValue('收款人'),
    InfoChecker: AppData.getSingleValue('复核人'),
    InfoInvoicer: Util.f.name,
    InfoNotes: data.f_note ? data.f_note : '',
    Details: []
   }
   let basebill = Object.assign({}, params)
   let otherbill = Object.assign({}, params)
   let detail
   if (data.f_bill_type === '欠费用户') {
     detail = yield co(Vue.resetpost('rs/sql/singleTable_OrderBy', {data: {items: '*', tablename: 't_chargeprice',condition:'f_hand_id = ' + "'" + data.handplan_id + "'" + ' and f_state = ' + "'" + '有效' + "'",
                  orderitem: 'id'}}, {resolveMsg: '获取明细成功,正在传递数据给打印服务', rejectMsg: '获取明细失败'}))
   } else {
     detail = yield co(Vue.resetpost('rs/sql/singleTable_OrderBy', {data: {items: '*', tablename: 't_chargeprice',condition:'f_charge_id = ' + "'" + data.f_sell_id + "'" + ' and f_state = ' + "'" + '有效' + "'",
                  orderitem: 'id'}}, {resolveMsg: '获取明细成功,正在传递数据给打印服务', rejectMsg: '获取明细失败'}))
   }
   let pricename =  yield co(Vue.resetpost('rs/sql/singleTable_OrderBy', {data: {items: '*', tablename: 't_stairprice',condition:'f_userprice_id = ' + "'" + detail.data[0].f_userprice_id + "'" + ' and f_state = ' + "'" + '有效' + "'",
                orderitem: 'id'}}, {resolveMsg: null, rejectMsg: '获取明细失败'}))
  let newArr = splitArr(detail.data, pricename)
  basebill.Details = newArr[0]
  otherbill.Details = newArr[1]
  let basevalue = yield co(Vue.resetpost('http://127.0.0.1:8003/print', basebill, {resolveMsg: null, rejectMsg: '票据打印服务错误'}))
  // let basevalue = {
  //   InfoTypeCode: 1,
  //   InfoNumber: 2,
  //   retCode: 0
  // }
  if (basevalue) {
    let billdetail = {
      f_billcoll_id: basevalue.InfoTypeCode,
      f_bill_id: basevalue.InfoNumber,
      f_userinfo_id: data.f_userinfo_id,
      f_user_id: data.f_user_id,
      f_user_name: data.f_user_name,
      f_hand_id: data.f_hand_id,
      f_charge_id: data.id,
      f_total_money: data.f_total_money,
      f_paid_money: data.f_total_money,
      f_bill_type: data.f_bill_type,
      f_bill_style: data.f_bill_style[0],
      f_bill_state: '有效',
      f_operator: Util.f.name,
      f_filiale: Util.f.f_fengongsi,
      f_outlets: Util.f.f_parentname,
      f_cause: data.f_cause
    }
    if (basevalue.retCode === 0) {
      billdetail.f_print_state = '成功'
    } else if (basevalue.retCode === 1) {
      billdetail.f_print_state = '失败'
    }
    let basedetail = yield co(Vue.resetpost('rs/entity/t_billdetail', billdetail, {resolveMsg: '打印发票成功', rejectMsg: '打印发票失败'}))
    if (basedetail) {
      let othervalue = yield co(Vue.resetpost('http://127.0.0.1:8003/print', otherbill, {resolveMsg: null, rejectMsg: '票据打印服务错误'}))
      // let othervalue = {
      //   InfoTypeCode: 1,
      //   InfoNumber: 2,
      //   retCode: 0
      // }
      let billdetail = {
        f_billcoll_id: othervalue.InfoTypeCode,
        f_bill_id: othervalue.InfoNumber,
        f_userinfo_id: data.f_userinfo_id,
        f_user_id: data.f_user_id,
        f_user_name: data.f_user_name,
        f_hand_id: data.f_hand_id,
        f_charge_id: data.id,
        f_total_money: data.f_total_money,
        f_paid_money: data.f_total_money,
        f_bill_type: data.f_bill_type,
        f_bill_style: data.f_bill_style[0],
        f_bill_state: '有效',
        f_operator: Util.f.name,
        f_filiale: Util.f.f_fengongsi,
        f_outlets: Util.f.f_parentname,
        f_cause: data.f_cause
      }
      if (othervalue.retCode === 0) {
        billdetail.f_print_state = '成功'
      } else if (othervalue.retCode === 1) {
        billdetail.f_print_state = '失败'
      }
      return co(Vue.resetpost('rs/entity/t_billdetail', billdetail, {resolveMsg: '打印发票成功', rejectMsg: '打印发票失败'}))
    }
  }
}
let reissuetKingTaxBillGen = function * (data) {
  let tax = yield co(Vue.resetpost('rs/sql/singleTable_OrderBy', {data: {items: '*', tablename: 't_userinfo',
  condition:'f_userinfo_id = ' + "'" + data.f_userinfo_id + "'",
               orderitem: 'f_userinfo_id'}}, {resolveMsg: null, rejectMsg: null}))
  let params = {
    InfoKind: data.f_bill_style[0],
    InfoSellerAddressPhone: AppData.getSingleValue('售方地址及电话'),
    InfoSellerBankAccount: AppData.getSingleValue('售方开户行及账号'),
    InfoClientName: tax.data[0].f_user_name ? tax.data[0].f_user_name : '',
    InfoClientTaxCode: tax.data[0].f_taxpayer_id ? tax.data[0].f_taxpayer_id : '',
    InfoClientBankAccount: tax.data[0].f_paper_account ? tax.data[0].f_paper_account : '',
    InfoClientAddressPhone: tax.data[0].f_address_phone ? tax.data[0].f_address_phone : '',
    InfoCashier: AppData.getSingleValue('收款人'),
    InfoChecker: AppData.getSingleValue('复核人'),
    InfoInvoicer: Util.f.name,
    InfoNotes: data.f_note ? data.f_note : '',
    Details: []
   }
   let basebill = Object.assign({}, params)
   let otherbill = Object.assign({}, params)
   let detail= yield co(Vue.resetpost('rs/sql/singleTable_OrderBy', {data: {items: '*', tablename: 't_chargeprice',condition:'f_charge_id = ' + "'" + data.id + "'" + ' and f_state = ' + "'" + '有效' + "'",
                 orderitem: 'id'}}, {resolveMsg: '获取明细成功,正在传递数据给打印服务', rejectMsg: '获取明细失败'}))
  let pricename =  yield co(Vue.resetpost('rs/sql/singleTable_OrderBy', {data: {items: '*', tablename: 't_stairprice',condition:'f_userprice_id = ' + "'" + detail.data[0].f_userprice_id + "'" + ' and f_state = ' + "'" + '有效' + "'",
                orderitem: 'id'}}, {resolveMsg: null, rejectMsg: '获取明细失败'}))
  let newArr = splitArr(detail.data, pricename)
  basebill.Details = newArr[0]
  otherbill.Details = newArr[1]

   let selldetail = yield co(Vue.resetpost('rs/sql/singleTable_OrderBy', {data: {orderitem: 'id', items: '*', tablename: 't_sellingwater', condition: 'id = ' + data.id}}, {resolveMsg: null, rejectMsg: null}))
    data.billdetail = selldetail.data[0]
   let basevalue = yield co(Vue.resetpost('http://127.0.0.1:8003/print', basebill, {resolveMsg: null, rejectMsg: '票据打印服务错误'}))
   // let basevalue = {
   //  InfoTypeCode: 1,
   //  InfoNumber: 2,
   //  retCode: 0
   // }
   if (basevalue) {
    let billdetail = {
      f_billcoll_id: basevalue.InfoTypeCode,
      f_bill_id: basevalue.InfoNumber,
      f_userinfo_id: data.billdetail.f_userinfo_id,
      f_user_id: data.billdetail.f_user_id,
      f_user_name: data.billdetail.f_user_name,
      f_hand_id: data.billdetail.f_hand_id,
      f_charge_id: data.billdetail.f_charge_id,
      f_total_money: data.billdetail.f_preamount,
      f_paid_money: data.billdetail.f_collection,
      f_bill_type: data.f_bill_type,
      f_bill_style: data.f_bill_style[0],
      f_bill_state: '有效',
      f_operator: Util.f.name,
      f_filiale: Util.f.f_fengongsi,
      f_outlets: Util.f.f_parentname,
      f_cause: data.f_cause
    }
    if (basevalue.retCode === 0) {
      billdetail.f_print_state = '成功'
    } else if (basevalue.retCode === 1) {
      billdetail.f_print_state = '失败'
    }
    let basedetail = yield co(Vue.resetpost('rs/entity/t_billdetail', billdetail, {resolveMsg: '打印发票成功', rejectMsg: '打印发票失败'}))
    if (basedetail) {
      let othervalue = yield co(Vue.resetpost('http://127.0.0.1:8003/print', otherbill, {resolveMsg: null, rejectMsg: '票据打印服务错误'}))
      // let othervalue = {
      //   InfoTypeCode: 1,
      //   InfoNumber: 2,
      //   retCode: 0
      // }
      let billdetail = {
        f_billcoll_id: othervalue.InfoTypeCode,
        f_bill_id: othervalue.InfoNumber,
        f_userinfo_id: data.billdetail.f_userinfo_id,
        f_user_id: data.billdetail.f_user_id,
        f_user_name: data.billdetail.f_user_name,
        f_hand_id: data.billdetail.f_hand_id,
        f_charge_id: data.billdetail.f_charge_id,
        f_total_money: data.billdetail.f_preamount,
        f_paid_money: data.billdetail.f_collection,
        f_bill_type: data.f_bill_type,
        f_bill_style: data.f_bill_style[0],
        f_bill_state: '有效',
        f_operator: Util.f.name,
        f_filiale: Util.f.f_fengongsi,
        f_outlets: Util.f.f_parentname,
        f_cause: data.f_cause
      }
      if (othervalue.retCode === 0) {
        billdetail.f_print_state = '成功'
      } else if (othervalue.retCode === 1) {
        billdetail.f_print_state = '失败'
      }
      return co(Vue.resetpost('rs/entity/t_billdetail', billdetail, {resolveMsg: '打印发票成功', rejectMsg: '打印发票失败'}))
    }
  }
}

function splitArr(arr, pricename) {
  let arrBase = []
  let arrOther = []
  arr.forEach((item) => {
   if (item.f_price_type === '基本水价') {
     let value1 = {
       ListGoodsName: item.f_price_type.replace('价','费'),
       ListStandard: pricename.data[0].f_price_name,
       ListPrice: item.f_price,
       ListUnit: '吨',
       ListNumber: item.f_water,
       ListPriceKind: '1',
       InfoTaxRate: AppData.getSingleValue(`${item.f_price_type.replace('价','费')}税率`) ? AppData.getSingleValue(`${item.f_price_type.replace('价','费')}税率`) : '0'
     }
     arrBase.push(value1)
   } else {
     let value2 = {
       ListGoodsName: item.f_price_type.replace('价','费'),
       ListStandard: pricename.data[0].f_price_name,
       ListPrice: item.f_price,
       ListUnit: '吨',
       ListNumber: item.f_water,
       ListPriceKind: '1',
       InfoTaxRate: AppData.getSingleValue(`${item.f_price_type.replace('价','费')}税率`) ? AppData.getSingleValue(`${item.f_price_type.replace('价','费')}税率`) : '0'
     }
     arrOther.push(value2)
   }

  })
  return [arrBase, arrOther]
}
let KingTaxBillService = {
  install (Vue, options) {
    // 给vue增添对话框显示方法
    Vue.KingTaxBillService = Vue.prototype.$KingTaxBillService = KingTaxBillService
  },
  printKingTaxBill (data) {
    return co(printKingTaxBillGen(data))
  },
  reissuetKingTaxBill (data) {
    return co(reissuetKingTaxBillGen(data))
  }
}

export default KingTaxBillService
